<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Data segment</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Data_segment"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Data_segment rootpage-Data_segment skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Data segment</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>In <a href="Computing" title="Computing">computing</a>, a <b>data segment</b> (often denoted <b>.data</b>) is a portion of an <a href="Object_file" title="Object file">object file</a> or the corresponding <a href="Address_space" title="Address space">address space</a> of a program that contains initialized <a href="Static_variable" title="Static variable">static variables</a>, that is, <a href="Global_variable" title="Global variable">global variables</a> and <a href="Static_local_variable" class="mw-redirect" title="Static local variable">static local variables</a>. The size of this segment is determined by the size of the values in the program's source code, and does not change at <a href="Run_time_(program_lifecycle_phase)" class="mw-redirect" title="Run time (program lifecycle phase)">run time</a>.
</p><p>The data segment is read/write, since the values of variables can be altered at run time. This is in contrast to the <i>read-only data segment</i> (<i><style data-mw-deduplicate="TemplateStyles:r1238216509">
/* start https://en.wikipedia.org/ */
.mw-parser-output .vanchor>:target~.vanchor-text{background-color:#b1d2ff}@media screen{html.skin-theme-clientpref-night .mw-parser-output .vanchor>:target~.vanchor-text{background-color:#0f4dc9}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .vanchor>:target~.vanchor-text{background-color:#0f4dc9}}
/* end https://en.wikipedia.org/ */
</style><span class="vanchor"><span class="vanchor-text">rodata</span></span> segment</i> or <i>.rodata</i>), which contains static constants rather than variables; it also contrasts to the <a href="Code_segment" title="Code segment">code segment</a>, also known as the text segment, which is read-only on many architectures. Uninitialized data, both variables and constants, is instead in the <a href=".bss" title=".bss">.bss</a> segment.
</p><p>Historically, to be able to support memory address spaces larger than the native size of the internal address register would allow, early CPUs implemented a system of segmentation whereby they would store a small set of indexes to use as offsets to certain areas. The <a href="Intel_8086" title="Intel 8086">Intel 8086</a> family of CPUs provided four segments: the code segment, the data segment, the stack segment and the extra segment. Each segment was placed at a specific location in memory by the software being executed and all instructions that operated on the data within those segments were performed relative to the start of that segment. This allowed a 16-bit address register, which would normally be able to access 64 KB of memory space, to access 1 MB of memory space.
</p><p>This segmenting of the memory space into discrete blocks with specific tasks carried over into the programming languages of the day and the concept is still widely in use within modern programming languages.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Program_memory">Program memory</h2></div>
<p>A computer program memory can be largely categorized into two sections: read-only and read/write. This distinction grew from early systems holding their main program in <a href="Read-only_memory" title="Read-only memory">read-only memory</a> such as <a href="Mask_ROM" class="mw-redirect" title="Mask ROM">Mask ROM</a>, <a href="EPROM" title="EPROM">EPROM</a>, <a href="Programmable_read-only_memory" class="mw-redirect" title="Programmable read-only memory">PROM</a> or <a href="EEPROM" title="EEPROM">EEPROM</a>. As systems became more complex and programs were loaded from other media into RAM instead of executing from ROM, the idea that some portions of the program's memory should not be modified was retained. These became the <i>.text</i> and <i>.rodata</i> segments of the program, and the remainder which could be written to divided into a number of other segments for specific tasks.
</p>
<div class="mw-heading mw-heading3"><h3 id="Code">Code</h3></div>
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Code_segment" title="Code segment">Code segment</a></div>
<p>The <b>code segment</b>, also known as <b>text segment</b>, contains <a href="Executable" title="Executable">executable</a> code and is generally read-only and fixed size.
</p>
<div class="mw-heading mw-heading3"><h3 id="Data">Data</h3></div>
<p>The <b>data segment</b> contains initialized static variables, i.e. global variables and local static variables which have a defined value and can be modified. Examples in C include:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span>
<span class="kt">char</span><span class="w"> </span><span class="n">a</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"Hello World"</span><span class="p">;</span>
<span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2023</span><span class="p">;</span><span class="w"> </span><span class="c1">// Initialized static global variable</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">foo</span><span class="w"> </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2023</span><span class="p">;</span><span class="w"> </span><span class="c1">// Initialized static local variable</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="BSS">BSS</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="BSS_segment" class="mw-redirect" title="BSS segment">BSS segment</a></div>
<p>The <b>BSS segment</b> contains uninitialized static data, both variables and constants, i.e. global variables and local static variables that are initialized to zero or do not have explicit initialization in source code. Examples in C include:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="k">static</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="mi">12</span><span class="p">];</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Heap">Heap</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Manual_memory_management" title="Manual memory management">Manual memory management</a></div>
<p>The <b>heap segment</b> contains dynamically allocated memory, commonly begins at the end of the BSS segment and grows to larger addresses from there. It is managed by <a href="Malloc" class="mw-redirect" title="Malloc">malloc</a>, calloc, realloc, and free, which may use the <a href="Sbrk" title="Sbrk">brk</a> and <a href="Sbrk" title="Sbrk">sbrk</a> system calls to adjust its size (note that the use of brk/sbrk and a single heap segment is not required to fulfill the contract of malloc/calloc/realloc/free; they may also be implemented using <a href="Mmap" title="Mmap">mmap</a>/munmap to reserve/unreserve potentially non-contiguous regions of virtual memory into the process' <a href="Virtual_address_space" title="Virtual address space">virtual address space</a>). The heap segment is shared by all threads, shared libraries, and dynamically loaded modules in a process.
</p>
<div class="mw-heading mw-heading3"><h3 id="Stack">Stack</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Call_stack" title="Call stack">Call stack</a></div>
<p>The <b>stack segment</b> contains the <a href="Call_stack" title="Call stack">call stack</a>, a <a href="LIFO_(computing)" class="mw-redirect" title="LIFO (computing)">LIFO</a> structure, typically located in the higher parts of memory. A "stack pointer" register tracks the top of the stack; it is adjusted each time a value is "pushed" onto the stack. The set of values pushed for one function call is termed a "stack frame". A stack frame consists at minimum of a return address. <a href="Automatic_variable" title="Automatic variable">Automatic variables</a> are also allocated on the stack.
</p><p>The stack segment traditionally adjoined the heap segment and they grew towards each other; when the stack pointer met the heap pointer, free memory was exhausted. With large address spaces and virtual memory techniques they tend to be placed more freely, but they still typically grow in a converging direction. On the standard PC <a href="X86_architecture" class="mw-redirect" title="X86 architecture">x86 architecture</a> the stack grows toward address zero, meaning that more recent items, deeper in the call chain, are at numerically lower addresses and closer to the heap. On some other architectures it grows the opposite direction.
</p>
<div class="mw-heading mw-heading2"><h2 id="Interpreted_languages">Interpreted languages</h2></div>
<p>Some interpreted languages offer a similar facility to the data segment, notably <a href="Perl" title="Perl">Perl</a><sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> and <a href="Ruby_(programming_language)" title="Ruby (programming language)">Ruby</a>.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> In these languages, including the line <code>__DATA__</code> (Perl) or <code>__END__</code> (Ruby, old Perl) marks the end of the code segment and the start of the data segment. Only the contents prior to this line are executed, and the contents of the source file after this line are available as a file object: <code>PACKAGE::DATA</code> in Perl (e.g., <code>main::DATA</code>) and <code>DATA</code> in Ruby. This can be considered a form of <a href="Here_document" title="Here document">here document</a> (a file literal).
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Segmentation_(memory)" class="mw-redirect" title="Segmentation (memory)">Segmentation (memory)</a></li>
<li><a href="Segmentation_fault" title="Segmentation fault">Segmentation fault</a></li>
<li><a href="Linker_(computing)" title="Linker (computing)">Linker (computing)</a></li>
<li><a href="Code_segment" title="Code segment">Code segment</a></li>
<li><a href=".bss" title=".bss">.bss</a></li>
<li><a href="Uninitialized_variable" title="Uninitialized variable">Uninitialized variable</a></li>
<li><a href="Stack_(abstract_data_type)" title="Stack (abstract data type)">Stack (abstract data type)</a></li>
<li><a href="Process_control_block" title="Process control block">Process control block</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://perldoc.perl.org/perldata.html#Special-Literals">perldata: Special Literals</a></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text">Ruby: Embedded Data: <a rel="nofollow" class="external autonumber" href="https://docs.ruby-lang.org/en/master/globals_rdoc.html#label-Embedded+Data">[1]</a></span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.bravegnu.org/gnu-eprog/c-startup.html">"C startup"</a>. <i>bravegnu.org</i>.</cite></li>
<li><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20090202113414/http://blog.ooz.ie/2008/09/0x03-notes-on-assembly-memory-from.html">"mem_sequence.c - sequentially lists memory regions in a process"</a>. Archived from <a rel="nofollow" class="external text" href="http://blog.ooz.ie/2008/09/0x03-notes-on-assembly-memory-from.html">the original</a> on 2009-02-02.</cite></li>
<li><cite id="CITEREFvan_der_Linden1997" class="citation book cs1">van der Linden, Peter (1997). <a rel="nofollow" class="external text" href="http://www.electroons.com/8051/ebooks/expert%20C%20programming.pdf"><i>Expert C Programming: Deep C Secrets</i></a> <span class="cs1-format">(PDF)</span>. Prentice Hall. pp. 119ff.</cite></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-04-29" href="https://en.wikipedia.org/wiki/?title=Data_segment&oldid=1287920597">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>